﻿#include "OperVAOneXMLImport.h"
#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include "FITK_Kernel/FITKAppFramework/FITKAppSettings.h"
#include "FITK_Kernel/FITKAppFramework/FITKComponents.h"
#include "FITK_Kernel/FITKCore/FITKAbstractDataObject.h"
#include "FITK_Kernel/FITKCore/FITKThreadPool.h"
#include "FITK_Component/FITKVAOneXMLIO/FITKVAOneXMLIOInterface.h"
#include "FITK_Component/FITKAcousticsModel/FITKAbstractSEASubSys.h"
#include "FITK_Component/FITKAcousticsModel/FITKAcousticsDiscModel.h"
#include "FITK_Component/FITKAcousticsModel/FITKAcousticsSEAModel.h"
#include "FITK_Component/FITKAcousticsModel/FITKSEASubSysAbsShell.h"
#include "FITK_Component/FITKAcousticsModel/FITKAbstractSEAFace.h"
#include "FITK_Component/FITKAcousticsModel/FITKAcousticsDiscNodeList.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsPhysics.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsAbsPhysicsProp.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsAbsMaterial.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsAbsNCT.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsSpectraManager.h"
#include "OperatorsInterface/GraphEventOperator.h"
#include "GUIFrame/MainWindow.h"
#include <QFileDialog>

namespace ModelOper
{
    bool OperVAOneXMLImport::execGUI()
    {
        //选择文件
        this->clearArgs();
        //主界面
        QWidget* mw = FITKAPP->getGlobalData()->getMainWindow();
        //获取工作目录
        QString wk;
        AppFrame::FITKAppSettings* settings = FITKAPP->getAppSettings();
        if (settings)
            wk = settings->getWorkingDir();

        QFileDialog dlg(mw, QObject::tr("Import XML File"), wk, "XML(*.xml)");
        dlg.setAcceptMode(QFileDialog::AcceptOpen);  //打开模式
        dlg.show();
        //阻塞，否则键盘事件处理会出问题
        bool accept = false;
        QEventLoop loop;
        connect(&dlg, &QFileDialog::accepted, [&] {loop.quit(); accept = true;  });
        connect(&dlg, &QFileDialog::rejected, [&] {loop.quit(); accept = false;  });
        loop.exec();

        if (!accept) return false;
        QStringList files = dlg.selectedFiles(); //选择的文件名称
        if (files.isEmpty()) return false;
        QString fileName = files.at(0);

        if (fileName.isEmpty())
            return false;
        //存储信息
        if (_emitter == nullptr) return false;

        this->setArgs("FileName", fileName);
        return true;
    }



    bool OperVAOneXMLImport::execProfession()
    {
        //导入文件名
        QString fileName;
        if (!this->argValue<QString>("FileName", fileName) || fileName.isEmpty()) return false;

        // Added by liuzhonghua. 主线程移除数据。
        //@{
        Acoustics::FITKAcousticsPhysics* physics = FITKAPP->getGlobalData()->getPhysicsData<Acoustics::FITKAcousticsPhysics>();
        Acoustics::FITKAcousticsDiscModel* discModel = FITKAPP->getGlobalData()->getMeshData<Acoustics::FITKAcousticsDiscModel>();
        if (!discModel || !physics)return false;
        discModel->clearDiscModel();
        physics->clearAcousticsPhysics();
        //@}

        //读写组件获取
        VAOne::FITKVAOneXMLIOInterface* xmlIO = FITKAPP->getComponents()->
            getComponentTByName<VAOne::FITKVAOneXMLIOInterface>("VAOneXMLIO");

        //等待线程结束
        Core::FITKThreadPool::getInstance()->wait();

        if (xmlIO == nullptr) return false;
        //清理信号关联
        QObject::disconnect(xmlIO, SIGNAL(ioThreadFinishedSig()), nullptr, nullptr);

        GUI::MainWindow* mw = FITKAPP->getGlobalData()->getMainWindowT<GUI::MainWindow>();
        if (mw)
            mw->setProcessLabel(QString(tr("Reading %1")).arg(fileName));

        //获取要存储的数据位置
        //传入参数
        xmlIO->setDataObject("Model", discModel);
        xmlIO->setDataObject("Physics", physics);
        xmlIO->setFileName(fileName);
        xmlIO->runInThread();
        //关联结束读取槽函数
        connect(xmlIO, SIGNAL(ioThreadFinishedSig()), this, SLOT(ioThreadFinishedSlot()), Qt::UniqueConnection);
        //执行读取
        xmlIO->exec(1);

        //保存执行的脚本
        QString scr = QString("Global.Files.ImportXMLFile('%1')").arg(fileName);
        this->saveScript(scr);

        return xmlIO->isSuccess();
    }

    void OperVAOneXMLImport::ioThreadFinishedSlot()
    {
        VAOne::FITKVAOneXMLIOInterface* xmlIO = FITKAPP->getComponents()->
            getComponentTByName<VAOne::FITKVAOneXMLIOInterface>("VAOneXMLIO");
        if (xmlIO == nullptr) return;
        //取消关联
        disconnect(xmlIO, SIGNAL(ioThreadFinishedSig()), this, SLOT(ioThreadFinishedSlot()));

        ////获取离散数据
        //Acoustics::FITKAcousticsDiscModel* discModel = FITKAPP->getGlobalData()->getMeshData<Acoustics::FITKAcousticsDiscModel>();
        //if (!discModel)return;
        ////获取SEA点数据
        //Acoustics::FITKAcousticsSEAModel* seaModel = discModel->getSEAModel();
        //if (!seaModel)return;
        //Acoustics::FITKAcousticsSEASubSysManager* subSysMgr = seaModel->getSEASubSysManager();
        //if (!subSysMgr) return;
        //int count = subSysMgr->getDataCount();
        EventOper::GraphEventOperator* graphOper = FITKOPERREPO->getOperatorT<EventOper::GraphEventOperator>("GraphPreprocess");
        if (graphOper)
        {
            graphOper->updateAll(true);
        }
    }

}
